#!/bin/bash

#Se chequea que todo este bien
if [ $# -ne "1" ]
then
	echo "USO: ${0} <SERVER_NAME>"
	exit 0
fi

SERVER_NAME=${1}

source config.sh  # incluyo las constantes

if [ "${SERVER_NAME}" == "WEBSERVER" ]
then
	SERVER=("${WEBSERVER[@]}")

elif [ "${SERVER_NAME}" == "FTPSERVER" ]
then
	SERVER=("${FTPSERVER[@]}")

elif [ "${SERVER_NAME}" == "TELSERVER1" ]
then
	SERVER=("${TELSERVER1[@]}")

elif [ "${SERVER_NAME}" == "TELSERVER2" ]
then
	SERVER=("${TELSERVER2[@]}")

elif [ "${SERVER_NAME}" == "HOSTA" ]
then
	SERVER=("${HOSTA[@]}")

elif [ "${SERVER_NAME}" == "HOSTB" ]
then
	SERVER=("${HOSTB[@]}")

elif [ "${SERVER_NAME}" == "HOSTC" ]
then
	SERVER=("${HOSTC[@]}")

else
	echo "El server debe ser: 'WEBSERVER', 'FTPSERVER', 'TELSERVER1', 'TELSERVER2', 'HOSTA', 'HOSTB' O 'HOSTC'"
	exit 1
fi

TAP=${SERVER[0]}
HOST_IP=${SERVER[1]}
NETMASK=${SERVER[2]}
PORT_NUM=${SERVER[3]}

#Crea una interfaz "tap" ethernet que va a ser usada por el tunel
sudo openvpn --mktun --dev ${TAP}
sudo ifconfig ${TAP} 0.0.0.0 promisc up

#Levanta el servidor openvpn
gnome-terminal --title=${SERVER_NAME} -x sudo openvpn --port ${PORT_NUM} --dev ${TAP} --ifconfig ${HOST_IP} ${NETMASK}


exit 0
